/*
 * 太原科技大学。
 * Copyright 2021, YCHS Information&Science Technology Co.,Ltd,
 * All rights reserved.
 */
package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import com.itheima.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liuchao
 * @version 1.0
 * @date 2024/3/23
 */
@Slf4j
@RestController
public class LoginController {

    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result loging(@RequestBody Emp emp){

      log.info("请求登录",emp);
      Emp e=empService.login(emp);
      //登录成功，生成下发令牌
        if(e!=null){
            //生成哈希值的map封装数据
            Map<String,Object> claims=new HashMap<>();
            claims.put("id",e.getId());
            claims.put("name",e.getName());
            claims.put("username",e.getUsername());

            String jwt= JWTUtils.setJwt(claims);//生成
            return Result.success(jwt);
        }


      //登录失败返回错误
      return Result.error("NOT_LOGIN");
    }
}
